package finalED.util.dataStructures.AVL;

/**
 *
 * @author Oswaldo.C
 */
public class AVLNode <E extends Object & Comparable>{
    
    /** A comparable <code>Object</code> wrapped into the tree node.*/
    private E data;

    /** Left sub-tree's root.*/
    private AVLNode<E> leftNode;
    
    /** Right sub-tree's root.*/
    private AVLNode<E> rightNode;

    /**
     * 
     */
    public AVLNode(){
       this(null);    
    }    
    
    /**
     * 
     * @param data 
     */
    public AVLNode(E data){
        this.data = data;
        leftNode = rightNode = null;
    }

    
    /**
     * @return the data
     */
    public E getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(E data) {
        this.data = data;
    }

    /**
     * @return the leftNode
     */
    public AVLNode<E> getLeftNode() {
        return leftNode;
    }

    /**
     * @return the rightNode
     */
    public AVLNode<E> getRightNode() {
        return rightNode;
    }

    /**
     * 
     * @param data 
     */
    public void setLeftData(E data){
        if ( leftNode == null )
            leftNode = new AVLNode<E>(data);
        else
            leftNode.setData(data);
    }
    
    /**
     * 
     * @param data 
     */
    public void setRightData(E data){
        if ( rightNode == null )
            rightNode = new AVLNode<E>(data);
        else
            rightNode.setData(data);   
    } 
    
    /**
     * 
     * @param tmp 
     */
    public void setLeftNode( AVLNode<E> tmp){
        leftNode = tmp;
    }
    
    /**
     * 
     * @param tmp 
     */
    public void setRightNode( AVLNode<E> tmp){
        rightNode = tmp;
    }
    
    /**
     * 
     * @return 
     */
    public int height(){
        int leftHeight = (leftNode == null)? 0 : leftNode.height();
        int rightHeight = (rightNode == null)? 0 : rightNode.height();
        
        return 1 + Math.max(leftHeight, rightHeight);
    }
    
    /**
     * 
     * @return 
     */
    public int getBalance(){
        int leftHeight = (leftNode == null)? 0 : leftNode.height();
        int rightHeight = (rightNode == null)? 0 : rightNode.height();
        
        return rightHeight - leftHeight;
    }
    
    
} //End Class AVLNode
